package com.heima.article.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.es.ApArticleSearch;
import com.heima.article.es.IApArticleRepository;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.minio.MinIOService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.BulkOptions;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.ls.LSInput;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private Configuration configuration;

    @Autowired
    IApArticleContentService contentService;

    @Autowired
    private MinIOService minIOService;

    @Autowired
    IApArticleService apArticleService;

    @GetMapping("/html/{id}")
    public ResponseResult createHtml(@PathVariable("id") Long id) throws IOException, TemplateException {

        Template template = configuration.getTemplate("article.ftl");

        LambdaQueryWrapper<ApArticleContent> query = new LambdaQueryWrapper<>();

        query.eq(ApArticleContent::getArticleId, id);

        ApArticleContent content = contentService.getOne(query);

        StringWriter out = new StringWriter();

        Map model = new HashMap();
        model.put("content", JSON.parseArray(content.getContent()));
        template.process(model, out);

        String name = id + ".html";

        InputStream in = new ByteArrayInputStream(out.toString().getBytes());

        String url = minIOService.upload(name, in, "text/html");

        LambdaUpdateWrapper<ApArticle> update = new LambdaUpdateWrapper<>();
        update.eq(ApArticle::getId, id);
        update.set(ApArticle::getStaticUrl, url);
        apArticleService.update(update);
        return ResponseResult.okResult(url);
    }

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @GetMapping("/es/index")
    public ResponseResult testIndex() {

        elasticsearchRestTemplate.createIndex(ApArticleSearch.class);

        return ResponseResult.okResult();
    }

    @GetMapping("/es/map")
    public ResponseResult testMap() {

      /*  elasticsearchRestTemplate.putMapping(ApArticleSearch.class);

        return ResponseResult.okResult();*/

        elasticsearchRestTemplate.putMapping(ApArticleSearch.class);
        return ResponseResult.okResult();
    }

    @GetMapping("/es/doc")
    public ResponseResult testDoc() {

        List<ApArticle> list = apArticleService.list();

        List<IndexQuery> queries = new ArrayList<>();

        for (ApArticle apArticle : list) {

            ApArticleSearch apArticleSearch = new ApArticleSearch();

            BeanUtils.copyProperties(apArticle, apArticleSearch);

            IndexQuery query = new IndexQuery();

            query.setId(apArticle.getId().toString());

            query.setObject(apArticleSearch);

            queries.add(query);
        }
        elasticsearchRestTemplate.bulkIndex(queries, BulkOptions.defaultOptions());

        return ResponseResult.okResult();
    }

    @Autowired
    private IApArticleRepository apArticleRepository;

    @GetMapping("/es/repo")
    public ResponseResult testRepository() {

        apArticleRepository.deleteAll();

        List<ApArticle> list = apArticleService.list();

        List<ApArticleSearch> apArticleSearches = new ArrayList<>();

        for (ApArticle apArticle : list) {

            ApArticleSearch apArticleSearch = new ApArticleSearch();

            BeanUtils.copyProperties(apArticle, apArticleSearch);

            apArticleSearches.add(apArticleSearch);
        }
        apArticleRepository.saveAll(apArticleSearches);

        return ResponseResult.okResult();
    }
}
